/*************************************************************************
 * AUTHOR		 : Chris Hayes
 * STUDENT ID	 : 656960
 * ASSIGNMENT #5 : Multi-Dimensional Array - Tic Tac Toe
 * CLASS		 : CS1B
 * SECTION		 : MW12:30
 * DUE DATE		 : June 15, 2011
 *************************************************************************/

#include "tictacheader.h"

/**************************************************************************
* FUNCTION GetAndCheckInp
*__________________________________________________________________________
* This is a very simple AI.  It randomly pics a row and a column and then
* 	checks the board to see if that location has been used.  If it has,
* 	it picks another two numbers randomly until it finds an open space.
* 	This function is used in GetAndCheckInput if a player is named computer
*__________________________________________________________________________
* PRE-CONDITIONS
*  row	 : Row should be previously defined
*  col	 : Col should be previously defined
*  board : board should be previously defined
*
* POST-CONDITIONS
*  It returns a row and column to be played in GetAndCheckInput
**************************************************************************/

void GetAndCheckInp(char board[][3],
					char token,
					string player1,
					string player2)
{
	bool invalid;
	int row;
	int col;
	string currentPlayer;
	string otherPlayer;
	string saveGame;

	if(token == 'X')
	{
		currentPlayer = player1;
		otherPlayer = player2;
	}
	else
	{
		currentPlayer = player2;
		otherPlayer = player1;
	}

	if(currentPlayer == "computer")
	{
		ComputerEasy(row, col, board);
	}
	else
	{
		cout << currentPlayer << ", where would you like to play: ";
		do
		{
			invalid = false;

			//check for failed input buffer
			if(!(cin >> row >> col))
			{
				cin.clear();
				invalid = true;
				row = NUM_ROWS + 1;
				col = NUM_COLS + 1;
			}
			cin.ignore(100, '\n');

			//decrement row and column since our array starts at [0][0]
			row--;
			col--;

			//output error for invalid row
			if (row < 0 || row >= 3)
			{
				cout << "Invalid row selection\n";
				invalid = true;
			}

			//output error for invalid column
			if(col < 0 || col >= 3)
			{
				cout << "Invalid column selection\n";
				invalid = true;
			}

			//output error instructions
			if(invalid)
			{
				cout << "Please pick a valid row and column: ";
			}

			//check for open spot to play
			if(!invalid && board[row][col] != ' ')
			{
				if(board[row][col] == token)
				{
					cout << "You have already played here, ";
					cout << "pick another spot: ";
					invalid = true;
				}
				else
				{
					cout << otherPlayer << " has already played here, ";
					cout << "pick another spot: ";
					invalid = true;
				}
			}
		}while(invalid);
	}

	board[row][col] = token;
	cout << endl;
}
